#include "strmanip.h"

void mnp::trim(std::string& toTrim, const std::string trimToken)
{
	if ((toTrim.empty()) || (trimToken.empty())) return;
	int ocurrency = toTrim.find(trimToken);
	if (ocurrency == -1) return;
	toTrim.erase(ocurrency, trimToken.length());
}

void mnp::trim(std::string& toTrim, const std::string firstTrimToken, const std::string lastTrimToken)
{
	mnp::trim(toTrim, firstTrimToken);
	mnp::trim(toTrim, lastTrimToken);
}

void mnp::trim(std::vector<std::string>& toTrim, const std::string firstTrimToken, const std::string lastTrimToken)
{
	std::vector<std::string>::iterator iterator;
	for (iterator = toTrim.begin(); iterator != toTrim.end(); ++iterator)
	{
		mnp::trim(*iterator, firstTrimToken);
		mnp::trim(*iterator, lastTrimToken);
	}
}

std::vector<std::string> mnp::slice(std::string toSlice, const std::string separatorToken)
{
	std::vector<std::string> fields;
	if ((toSlice.empty()) || (separatorToken.empty())) return fields;
	while (toSlice.length())
	{
		int ocurrency = toSlice.find(separatorToken);
		if (ocurrency != -1)
		{
			fields.push_back(toSlice.substr(0, ocurrency));
			toSlice.erase(0, ocurrency + separatorToken.length());
		}
		else
		{
			fields.push_back(toSlice.substr(0));
			toSlice.erase();
		}
	}
	return fields;
}

std::vector<std::vector<std::string> > slice(std::vector<std::string> toSlice, const std::string separatorToken)
{
	std::vector<std::vector<std::string> > fields;
	std::vector<std::string>::iterator iterator;
	for (iterator = toSlice.begin(); iterator != toSlice.end(); ++iterator)
	{
		fields.push_back(mnp::slice(*iterator, separatorToken));
	}
	return fields;
}
